package com.habdqn.easybuy.dao.impl;

import com.habdqn.easybuy.dao.BaseDao;
import com.habdqn.easybuy.dao.ShopDao;
import com.habdqn.easybuy.entity.Shop;
import com.habdqn.easybuy.entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Xing.Jiejian
 * @date 2020/12/2
 */
public class ShopDaoImpl extends BaseDao implements ShopDao {
    private String baseSelect = "SELECT id, user_id, product_id, product_quantity FROM shop";

    /**
     * 通过实体作为筛选条件查询
     *
     * @param userId 用户编号
     * @return 对象列表
     */
    @Override
    public List<Shop> queryAll(int userId) {
        String sql = baseSelect + " WHERE user_id=?";
        return getListByRS(executeQuery(sql,userId));
    }

    /**
     * 根据编号查询购物车记录
     *
     * @param ids 编号，多个之间用","分割
     * @return 购物车记录
     */
    @Override
    public List<Shop> queryByIds(String ids) {
        String sql = baseSelect + " WHERE id IN( "+ids+" )";
        return getListByRS(executeQuery(sql));
    }

    /**
     * 查询用户对应的商品购物车记录
     *
     * @param userId    用户编号
     * @param productId 商品编号
     * @return 购物车记录
     */
    @Override
    public Shop queryByUserAndProduct(int userId, int productId) {
        String sql =  baseSelect + " WHERE user_id=? AND product_id=?";
        List<Shop> shops = getListByRS(executeQuery(sql,userId,productId));
        if (shops!=null && shops.size()>0){
            return shops.get(0);
        }
        return null;
    }

    /**
     * @param id
     * @return
     */
    @Override
    public Shop queryById(int id) {
        String sql =  baseSelect + " WHERE id=?";
        List<Shop> shops = getListByRS(executeQuery(sql,id));
        if (shops!=null && shops.size()>0){
            return shops.get(0);
        }
        return null;
    }

    /**
     * 新增数据
     *
     * @param shop 实例对象
     * @return 影响行数
     */
    @Override
    public int insert(Shop shop) {
        String sql = "INSERT INTO shop(user_id, product_id, product_quantity) VALUES(?,?,?)";
        return executeInsert(sql,shop.getUserId(),shop.getProductId(),shop.getProductQuantity());
    }

    /**
     * 修改数据
     *
     * @param shop 实例对象
     * @return 影响行数
     */
    @Override
    public int update(Shop shop) {
        String sql = "UPDATE shop SET user_id=?, product_id=?, product_quantity=? WHERE id=?";
        return executeUpdate(sql,shop.getUserId(),shop.getProductId(),shop.getProductQuantity(),shop.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    @Override
    public int deleteById(Integer id) {
        String sql = "DELETE FROM shop WHERE id=?";
        return executeUpdate(sql,id);
    }

    /**
     * 从结果集中提取对象集合
     *
     * @param rs 结果集
     * @return 用户集合
     */
    private List<Shop> getListByRS(ResultSet rs) {
        List<Shop> shops = null;
        try {
            if (rs != null) {
                shops = new ArrayList<>();
                while (rs.next()) {
                    Shop shop = new Shop();
                    shop.setId(rs.getInt("id"));
                    shop.setUserId(rs.getInt("user_id"));
                    shop.setProductId(rs.getInt("product_id"));
                    shop.setProductQuantity(rs.getInt("product_quantity"));
                    shops.add(shop);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return shops;
    }
}
